// pages/bookdetailed/bookdetailed.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: '',
    detailData: [],

    scoreVal: 2.5,
    // 是否展开简介
    isShowIntro: false,
    // 更新时间
    updataTime: 0,
    showLoading: false,
    //  是否展示小说章节目录
    showCatalogue: false,
    // 小说目录数据
    chaptersData:[],
    // 小说目录popup的loding
    showCatalogueLoading:false,

    // 当前小说id
    sourceUuid:'',

    // 已读章节
    currentIndex:'',
    isDark:'',
    // 是否已加入书架
    isBookShelf:false
  },

  // 调用查找云函数
  getBook(id) {
    wx.cloud.callFunction({
      name: 'getBook',
      data: {
        bookId: id,
      }
    }).then((res) => {
      // console.log('成功', res);
      if (res.result.currentList.length != 0) {
        this.setData({
          isBookShelf: true
        })
      }
    })
  },
//  是否加入书架
  changeBookShelf() {
    if (!this.data.isBookShelf) {
      this.addBookShelfFn()


    } else {
      this.delBookShelfFn()
    }

  },
// 添加
  addBookShelfFn() {
    wx.cloud.callFunction({
      name: 'addBookShelf',
      data: {
        bookId: this.data.currentId,
        sourceUuid: this.data.sourceUuid,
        v_book: this.data.detailData.bookinfo.v_book,
        cover: this.data.detailData.bookinfo.cover_img_url,
        type:"bookShelf"
      }
    }).then(() => {
      // this.getBook()
      Toast.success('已加入书架');
      this.setData({
        isBookShelf: true
      })
    })
  },
// 删除
  delBookShelfFn() {
    wx.cloud.callFunction({
      name: 'delBookShelf',
      data: {
        bookId: this.data.currentId,
      }
    }).then(() => {
      // this.getBook()
      Toast.success('已移出书架');
      this.setData({
        isBookShelf: false
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let str =   {
      sourceUuid: "bd_bf122a3e2ed543bfa8603d2c20acefdc_4",
    }
    wx.setStorageSync('storageData', str)
    

    let storageDark = wx.getStorageSync('storageDark') ? wx.getStorageSync('storageDark') : false

    this.setData({
      currentId: options.id,
      showLoading: true,
      isDark: storageDark
    })
    this.getBook(options.id)

    
    
    let currentReadChapters = wx.getStorageSync('readChapters') ? wx.getStorageSync('readChapters') : []

    currentReadChapters.map(t => {
      if (t.bookId == options.id) {
        this.setData({
          currentIndex: t.currentIndex
        })
      }
    })


    this.getDetailData()
  },

  getDetailData() {
    wx.request({
      url: `https://app.kujiang.com/v1/book/get_book_infos?book=${this.data.currentId}`,
      success: ({ data }) => {
        this.setData({
          detailData: data.body,
          scoreVal: (data.body.book_rate.score / 2).toFixed(1),
          showLoading: false
        })

        this.setUpdateTime(this.data.detailData.bookinfo.u_time)
        console.log(this.data.detailData);
        this.getBookId()
      }
    }
    )

  },

  // 获取小说id
  getBookId() {

    wx.request({
      url: `https://apis.netstart.cn/yunyuedu/search/book/data.json?key=${this.data.detailData.bookinfo.v_book}`,
      success: ({ data }) => {
        console.log(data.data);
        if (data.data.books.length==0) {
          // console.log(1);
          let currentSourceUuid = wx.getStorageSync('storageData')
          this.setData({
            sourceUuid:currentSourceUuid.sourceUuid
          })
          console.log(this.data.sourceUuid);


        }else {
          this.setData({
            sourceUuid:data.data.books[0].sourceUuid
          })

        }
        this.getChaptersData()

      }
    })
  },
 // 获取小说章节目录
  getChaptersData(){
    console.log(this.data.sourceUuid);
    wx.request({
      url: `https://apis.netstart.cn/yunyuedu/reader/book/info.json?source_uuid=${this.data.sourceUuid}`,
      success:({data})=>{
        this.setData({
          chaptersData:data.data.catalog,
          showCatalogueLoading:true
        })
        console.log(this.data.chaptersData);

      }
    })
  },

  changeShowIntro() {
    this.setData({
      isShowIntro: !this.data.isShowIntro
    })
  },

  setUpdateTime(time) {
    let num = parseInt((Date.now() - (time * 1000)) / 1000 / 60 / 60)

    if (num > 24) {
      let data = new Date(time * 1000)
      let y = data.getFullYear()
      let M = data.getMonth() + 1 >= 10 ? data.getMonth() + 1 : '0' + (data.getMonth() + 1)
      let d = data.getDate() >= 10 ? data.getDate() : '0' + data.getDate()
      num = `${y}-${M}-${d}`
    } else {
      num = num + '小时前'
    }

    this.setData({
      updataTime: num
    })
  },

  changeSHowCatalogue() {
    this.setData({
      showCatalogue: !this.data.showCatalogue
    })
  },

  goToReadingPage(){
    
    wx.navigateTo({
      url: `/pages/readingdetailed/readingdetailed?sourceUuid=${this.data.sourceUuid}&list=${JSON.stringify(this.data.chaptersData)}&bookName=${this.data.detailData.bookinfo.v_book}&bookId=${this.data.currentId}&cover=${this.data.detailData.bookinfo.big_cover_img_url}`,
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})